package com.coder520.POI.common.poiUtils.word;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.poi.POIXMLDocument;
import org.apache.poi.hssf.dev.EFBiffViewer;
import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import org.apache.poi.xwpf.usermodel.XWPFComment;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.xmlbeans.XmlException;

public class WordUtils {
	/**
	 * 创建word文件
	 * @param filePath
	 * @param fileName
	 *	2017年12月7日
	 */
	public static  void cretaeWord(String filePath,String fileName) {
		// TODO Auto-generated method stub
		File file=new File(filePath);
		//exists()测试此抽象路径名表示的文件或目录是否存在。
		//mkdir()创建此抽象路径名指定的目录。
		//mkdirs()创建此抽象路径名指定的目录，包括所有必需但不存在的父目录。
		if(!file.exists()) {
			file.mkdirs();
		}
		XWPFDocument document=new XWPFDocument();
		OutputStream stream=null;
		try {
			stream=new FileOutputStream(new File(file, fileName));
			document.write(stream);
		} catch (FileNotFoundException e) {
			// TODO: handle exception
			e.printStackTrace();
		}catch (IOException e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally {
			if (stream!=null) {
				try {
					stream.close();
				} catch (Exception e2) {
					// TODO: handle exception
					e2.printStackTrace();
				}
			}
		}
	}
	public static void writeDataDocx(String filePath,String data ,boolean jiacu,int size ) {
		InputStream iStream=null;
		OutputStream oStream=null;
		try {
			iStream=new FileInputStream(filePath);
			oStream=new FileOutputStream(filePath);
			XWPFDocument document=new XWPFDocument();
//			添加一个段落
			XWPFParagraph paragraph=document.createParagraph();
//			向左对齐
			paragraph.setAlignment(ParagraphAlignment.LEFT);
//			首行缩进
			paragraph.setIndentationFirstLine(400);
			paragraph.setIndentationHanging(400);
//			增加数据
			XWPFRun run=paragraph.createRun();
//			添加数据
			run.setText(data);
			run.setBold(jiacu);//加粗加黑
			run.setFontSize(size);//设置尺寸
			document.write(oStream);
			System.out.println("创建word文件成功");
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			if (iStream!=null) {
				try {
					iStream.close();
				} catch (Exception e2) {
					// TODO: handle exception
					e2.printStackTrace();
				}
			}
			if (oStream!=null) {
				try {
					oStream.close();
				} catch (Exception e2) {
					// TODO: handle exception
					e2.printStackTrace();
				}
			}
		}
	}
	public static StringBuffer readWord(String 	filePath) throws XmlException, OpenXML4JException {
		// TODO Auto-generated method stub
		StringBuffer 	content=new StringBuffer();
		InputStream 	iStream=null;
		try {
//			XWPFDocument doc=new XWPFDocument(iStream);
			XWPFWordExtractor document=new XWPFWordExtractor(POIXMLDocument.openPackage(filePath));
			System.out.println(document.getText()); 
//			System.out.println(doc.getProperties().getExtendedProperties().getUnderlyingProperties().getPages());
			} catch (FileNotFoundException e) {
			// TODO: handle exception
			e.printStackTrace();
		}catch (IOException e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally {
			if (iStream!=null) {
				try {
					iStream.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		return content;
	}
}
